/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : AntonymHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 15, 2008       Raghu Sarraff			       Created
 */
package lwf.handler.english;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import net.lwf.handler.util.ImageHtml;

import lwf.factory.PropertyReader;

/**
 * Type the class description here
 *  
 */
public class AntonymHandler extends WordHandler {
	
	private boolean headingdisplay = false;

	public AntonymHandler(int start, int end, int level) {
		init(start, end, level);
	}
	
	public AntonymHandler(int start, int end) {
		init(start, end, 0);
	}
	
	private void init(int start, int end, int level) {
		Map mapantonyms = (Map) PropertyReader.getInstance().getAntonyms();
		Map mapdata = null;
		while(mapdata == null) {
			mapdata = (Map) mapantonyms.get(String.valueOf(level));
			level = level - 1;
		}
		setDisplayData(start, end, this.toList(mapdata));
	}
	//THis code should go to property reader
	private List toList(Map mapdata) {
		List lstData = new ArrayList();
		if(mapdata != null) {
			Iterator itr = mapdata.entrySet().iterator();
			while(itr.hasNext()) {
				Map.Entry mapentry = (Map.Entry) itr.next();
				String key = (String) mapentry.getKey();
				String value = (String) mapentry.getValue();
				lstData.add(key + "," + value);
			}
		}
		return lstData;
	}
  
  protected String processRowData(Object obj) {
  	String data = (String) obj;
  	StringTokenizer stk = new StringTokenizer(data, ",");
  	String word = stk.nextToken();
  	String opposite = stk.nextToken();
  	
  	StringBuffer sbPData = new StringBuffer();
  	sbPData.append("<table border=\"0\" width=\"100%\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">");
  	    if(!headingdisplay) {
  		    sbPData.append("<tr>");
		        sbPData.append("<td width=\"10%\">&nbsp;</td>");
		        sbPData.append("<td width=\"35%\" class=\"tableHeader\" nowrap>Word</td>");		        
		        sbPData.append("<td width=\"5%\" class=\"tableHeader\" nowrap>&nbsp;</td>");
		        sbPData.append("<td width=\"35%\" class=\"tableHeader\" nowrap>Antonym</td>");
		        sbPData.append("<td width=\"15%\"> &nbsp; </td>");
	        sbPData.append("</tr>");
	        headingdisplay = true;
  	    }
	    sbPData.append("<tr>");
	        sbPData.append("<td width=\"10%\">");
		        sbPData.append(ImageHtml.getBulletImage(1));
	        sbPData.append("</td>");
	        sbPData.append("<td width=\"35%\" class=\"dataleft\" >");
	        	sbPData.append(word);
            sbPData.append("</td>");		        
	        sbPData.append("<td width=\"5%\" class=\"dataleft\" >");
        		sbPData.append("&nbsp;");
        	sbPData.append("</td>");
        	sbPData.append("<td width=\"35%\" class=\"dataleft\" >");
        		sbPData.append(opposite);
            sbPData.append("</td>");
	        sbPData.append("<td width=\"15%\"> &nbsp; </td>");
	    sbPData.append("</tr>");                
      sbPData.append("</table>");
      return sbPData.toString();
  } //processRowData

}

